package com.wangc.report.controller;

import com.wangc.report.entity.SysMenuEntity;
import com.wangc.report.service.SysMenuService;
import com.wangc.report.utils.PageUtils;
import com.wangc.report.utils.Query;
import com.wangc.report.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单管理Controller
 *
 * @author wangc
 * @email 1620415297@qq.com
 * @date 2020-04-01 23:34:07
 */
@RestController
@RequestMapping("sys/menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SysMenuEntity> sysMenuList = sysMenuService.queryList(query);
        int total = sysMenuService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysMenuList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{menuId}")
    public R info(@PathVariable("menuId") Long menuId) {
        SysMenuEntity sysMenu = sysMenuService.queryObject(menuId);

        return R.ok().put("sysMenu", sysMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SysMenuEntity sysMenu) {
        sysMenuService.save(sysMenu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SysMenuEntity sysMenu) {
        sysMenuService.update(sysMenu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] menuIds) {
        sysMenuService.deleteBatch(menuIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SysMenuEntity> list = sysMenuService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/user")
    public R user() {
        //SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(1l);
        return R.ok().put("menuList", menuList);
    }
}
